package com.api.gbhg.heyulei.dao.impl;

import com.api.gbhg.heyulei.action.DocumentAddAction;
import com.api.gbhg.heyulei.dao.DocumentAddDao;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.workflow.workflow.WorkflowVersion;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报盘文件存入实现类
 *
 * @author hyl
 * @version 1.0
 * @date 2021/4/22 16:41
 */
public class DocumentAddDaoImpl extends BaseBean implements DocumentAddDao {

    private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 根据mainTableId查询主表信息
     *
     * @param mainTableId
     * @return
     */
    @Override
    public String getMainTableName(Integer mainTableId) {
        RecordSet rs = new RecordSet();
        String sql = " select tablename from workflow_bill where id = " + mainTableId + " ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("tablename");
    }

    /**
     * 根据流程主表id获取流程详细表名称列表
     *
     * @param maindId
     * @return
     */
    @Override
    public List<String> getDetailTables(int maindId) {
        List<String> resList = new ArrayList<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("SELECT d.tablename FROM workflow_billdetailtable d where d.billid=" + maindId + " order by tablename asc;");
        while (rs.next()) {
            resList.add(rs.getString("tablename"));
        }
        return resList;
    }

    /**
     * 主表名查询表数据
     * @param mainTabName
     * @return
     */
    @Override
    public Map<String, Object> getMainTableMap(String mainTabName) throws IOException {
        Map<String, Object> map = new HashMap<>();

        //根据不同的表更改字段名
        String fieldName = "jbr";
        //读取配置文件
        String userFudsWorkflowId10 = WorkflowVersion.getActiveVersionWFID(getPropValue("ReimbursementWorkflowid", "YS10Workflowid"));
        //根据不同的表更改字段名
        if (VersionFlowUtil.getFlowTableName(userFudsWorkflowId10).equals(mainTabName)) {
            fieldName = "jbry";
        }
        RecordSet rs = new RecordSet();
        rs.executeQuery("SELECT * FROM " + mainTabName + "");
        while (rs.next()) {
            //requestId
            map.put("requestId", rs.getString("requestId"));
            //申请日期
            map.put("sqrq", rs.getString("sqrq"));
            //备注
            map.put("bz", rs.getString("bz"));
            //经办人
            map.put("jbr", rs.getInt(fieldName));
        }
        return map;
    }

    /**
     * 从配置文件中读取内容0
     * @param tableName 表名
     * @return
     * @throws IOException
     */
 /*
 private String getProperties(String tableName) throws IOException {
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = DocumentAddAction.class.getClassLoader().getResourceAsStream("com/api/gbhg/heyulei/properties/Document.properties");
        // 使用properties对象加载输入流
        properties.load(in);
        //从配置文件获取内容
        String proName = properties.getProperty(tableName);
        return proName;
    }
*/


    /**
     * 查询明细表数据
     *
     * @param flowDetailTableNames
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getDetailMap(List<String> flowDetailTableNames, String id) {
        Map<String, Object> map = new HashMap<>();

        //查询明细表1
        if (!flowDetailTableNames.get(0).isEmpty()) {
            RecordSet rs1 = new RecordSet();
            rs1.executeQuery("SELECT * FROM " + flowDetailTableNames.get(0) + " where mainid = " + id + "");
            while (rs1.next()) {
                //支付令
                map.put("zfl", rs1.getString("zfl"));
                //外币币种
                map.put("wbbz", rs1.getString("wbbz"));
                //预算来源
                map.put("ysly", rs1.getString("ysly"));
            }
        }

        //查询明细表2
        if (!flowDetailTableNames.get(0).isEmpty()) {
            RecordSet rs2 = new RecordSet();
            rs2.executeQuery("SELECT * FROM " + flowDetailTableNames.get(1) + " where mainid = " + id + "");
            while (rs2.next()) {
                //付款账号
                map.put("fkzh", rs2.getString("fkzh"));
                //付款银行
                map.put("fkyx", rs2.getString("fkyx"));
                //收款开户行
                map.put("skfkhx", rs2.getString("skfkhx"));
                //收款人账号
                map.put("skfzh", rs2.getString("skfzh"));
                //收款账号开户行
//            map.put("skfkhx",rs2.getString("skfkhx"));
                //金额
                map.put("zfje", rs2.getDouble("zfje"));
                //汇款方式
                map.put("zffs", rs2.getString("zffs"));
                //收款人
                map.put("skr", rs2.getString("skr"));
            }
        }

        //查询银行信息
        RecordSet rs3 = new RecordSet();
        rs3.executeQuery("SELECT * FROM uf_yhzhgltz where id = " + map.get("fkyx") + "");
        while (rs3.next()) {
            //付款账户名称
            map.put("zhm", rs3.getString("zhm"));
            //付款开户行
            map.put("khx", rs3.getString("khx"));
            //付款账号
            map.put("yxzh", rs3.getString("yxzh"));
        }

        return map;
    }

    /**
     * 新增报盘文件
     *
     * @param map
     */
    @Override
    public void addDocument(Map<String, Object> map) throws Exception {
        RecordSetTrans rs = new RecordSetTrans();
        String sql = null;
        switch (Integer.parseInt(map.get("fkyx").toString())) {
            //工商银行报盘文件
            case 1:
                sql = "INSERT INTO uf_gsyhbpwj(requestId,fkfs,fkzh,bza,skzh,skfmc,khwdmc,zfje,bzb,zfl,modedatacreater,modedatacreatedate,modedatamodifier,modedatamodifydatetime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                try {
                    rs.executeUpdate(sql,
                            map.getOrDefault("requestId", ""),
                            map.getOrDefault("zffs", ""),
                            map.getOrDefault("yxzh", ""),
                            map.getOrDefault("wbbz", ""),
                            map.getOrDefault("skfzh", ""),
                            map.getOrDefault("skr", ""),
                            map.getOrDefault("skfkhx", ""),
                            map.getOrDefault("zfje", ""),
                            map.getOrDefault("bz", ""),
                            map.getOrDefault("zfl", ""),
                            map.getOrDefault("jbr", ""),
                            map.getOrDefault("sqrq", ""),
                            map.getOrDefault("userId", ""),
                            sf.format(new Date()));
                } catch (Exception e) {
                    rs.rollback();
                    throw new Exception(e);
                }
                break;
            //中信银行报盘文件
            case 2:
                sql = "INSERT INTO uf_zxyhbpwj(requestId,zfl,fkzh,fkhm,skzh,skhm,skxmc,zzje,zclx,ysly,bz,modedatacreater,modedatacreatedate,modedatamodifier,modedatamodifydatetime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                try {
                    rs.executeUpdate(sql,
                            map.getOrDefault("requestId", ""),
                            map.getOrDefault("zfl", ""),
                            map.getOrDefault("yxzh", ""),
                            map.getOrDefault("khx", ""),
                            map.getOrDefault("skfzh", ""),
                            map.getOrDefault("skr", ""),
                            map.getOrDefault("skfkhx", ""),
                            map.getOrDefault("zfje", ""),
                            map.getOrDefault("zffs", ""),
                            map.getOrDefault("ysly", ""),
                            map.getOrDefault("bz", ""),
                            map.getOrDefault("jbr", ""),
                            map.getOrDefault("sqrq", ""),
                            map.getOrDefault("userId", ""),
                            sf.format(new Date()));
                } catch (Exception e) {
                    rs.rollback();
                    throw new Exception(e);
                }
                break;
            //农业银行报盘文件
            case 3:
                sql = "INSERT INTO uf_nyyhbpwj(requestId,zfl,skfzh,skfhm,sfnyyx,khyxxb,je,modedatacreater,modedatacreatedate,modedatamodifier,modedatamodifydatetime) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
                try {
                    rs.executeUpdate(sql,
                            map.getOrDefault("requestId", ""),
                            map.getOrDefault("zfl", ""),
                            map.getOrDefault("skfzh", ""),
                            map.getOrDefault("skfkhx", ""),
                            map.getOrDefault("1", ""),
                            map.getOrDefault("skfkhx", ""),
                            map.getOrDefault("zfje", ""),
                            map.getOrDefault("jbr", ""),
                            map.getOrDefault("sqrq", ""),
                            map.getOrDefault("userId", ""),
                            sf.format(new Date()));
                } catch (Exception e) {
                    rs.rollback();
                    throw new Exception(e);
                }
                break;
            //交通银行报盘文件
            case 4:
                sql = "INSERT INTO uf_jtyhbpwj(requestId,zfl,bz,rq,fkzhkhx,fkzhkh,fkzhmckmc,skzhkhx,skzh,je,bzxx,hkfs,modedatacreater,modedatacreatedate) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                try {
                    rs.executeUpdate(sql,
                            map.getOrDefault("requestId", ""),
                            map.getOrDefault("zfl", ""),
                            map.getOrDefault("wbbz", ""),
                            map.getOrDefault("sqrq", ""),
                            map.getOrDefault("khx", ""),
                            map.getOrDefault("yxzh", ""),
                            map.getOrDefault("zhm", ""),
                            map.getOrDefault("skfkhx", ""),
                            map.getOrDefault("skfzh", ""),
                            map.getOrDefault("zfje", ""),
                            map.getOrDefault("bz", ""),
                            map.getOrDefault("zffs", ""),
                            map.getOrDefault("jbr", ""),
                            map.getOrDefault("sqrq", "")
                    );

                } catch (Exception e) {
                    rs.rollback();
                    throw new Exception(e);
                }
                break;
            default:
                break;
        }

    }
}
